clear all;

% Set Parameters
order = 11;
mapDimension = 2^order+1;
initialVariance = 50; % initialVariance = floor(mapDimension/2) creates natural mages
h = .3; % Smoothness factor 0 to 1. 0 = max. roughness. 0.9 = natural
map = zeros(2^order+1)+inf;
map(1,1)=0; map(1,mapDimension)=0; map(mapDimension,1)=0; map(mapDimension,mapDimension)=0;

% Assign cell types to colors
cell_types{1} = struct('name',{{'Cell Type 3','Cell Type 2','Cell Type 1'}},'value',[6 50 175]); % must be entered monotonically
cell_types{2} = struct('name',{{'Arcade','Db Bouquet','Peptidergic','Sm Pyramidal'}},'value',[6 25 50 130]); % must be entered monotonically
cell_types{3} = struct('name',{{'Chandelier','Pyramidal'}},'value',[50 100]); % must be entered monotonically
cell_types{4} = struct('name',{{'Neuroglioform','Stellate','Lg Basket'}},'value',[6 50 175]); % must be entered monotonically
cell_types{5} = struct('name',{{'Chandelier','Lg Pyramidal'}},'value',[75 200]); % must be entered monotonically
cell_types{6} = struct('name',{{'Peptidergic','Pyramidal'}},'value',[100 175]); % must be entered monotonically
lyr = 2;

% Seeding
seed_initial = 1.4;
seed = seed_initial * initialVariance;

% Center Seed
% map(ceil(mapDimension/2),ceil(mapDimension/2)) = seed;

delta = 2^(order-2);
cstart = delta/2 + 1;
i = 1;
map(1+delta*i, cstart+delta : delta : mapDimension-delta) = seed;
map(1+delta/2 + delta*i , 1+delta : delta : mapDimension-delta) = seed;
i = i+1;
map(1+delta*i,cstart+delta : delta : mapDimension-delta) = seed;
map(1+delta/2 + delta*i , 1+delta : delta : mapDimension-delta) = seed;
i = i+1;
map(1+delta*i,cstart+delta : delta : mapDimension-delta) = seed;

% Create Map
if ~exist('map','var')
    map=createFractalTerrain(mapDimension,initialVariance,h);
else
    map=createFractalTerrain(mapDimension,initialVariance,h,map);
%     map=createFractalTerrainC(mapDimension,initialVariance,h,map); % C version
end

figure(1); clf;
plotMatrix3d(map,cell_types{lyr});
% plotMatrix3d(map);

% figure(2); clf;
% [r,c] = meshgrid(1:mapDimension , 1:mapDimension);
% map2 = map - 50;
% rng = find(map2 > -0.5 & map2 <=0.5);
% scatter(r(rng),c(rng),'o','markerfacecolor','flat');
% 
% map2 = map - 25;
% rng = find(map2 > -0.5 & map2 <=0.5);
% hold on
% scatter(r(rng),c(rng),'^','markerfacecolor','flat');

% ax = [207.4741  330.1875  328.4345  451.1479];
% axis(ax);
display({'min = ',min(min(map)),'max = ',max(max(map))});